package com.girlbook.controllers;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import net.paoding.rose.web.Invocation;
import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.annotation.rest.Post;
import net.paoding.rose.web.var.Model;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.girlbook.constants.GBookConstants;
import com.girlbook.model.Article;
import com.girlbook.page.PageView;
import com.girlbook.service.ArticleService;
import com.girlbook.tools.DateTool;

@Path("article")
public class ArticleController
{
	@Autowired
	private ArticleService articleService;
	
	/**
	 * 文章发布
	 * @return
	 * @throws Exception
	 */
	@Get("publish")
	public String publish() throws Exception
	{
		return "article/publish";
	}
	
	/**
	 * 文章保存
	 * @param inv
	 * @return
	 * @throws Exception
	 */
	@Post("save")
	public String save(Invocation inv) throws Exception
	{
		HttpServletRequest request = inv.getRequest();
		String title = request.getParameter("title");
		String summary = request.getParameter("summary");
		String content = request.getParameter("content");
		//参数校验
		if (StringUtils.isBlank(title) || StringUtils.isBlank(summary) || StringUtils.isBlank(content))
		{
			return "@参数不能为空";
		}
		//组装Article
		Article article = new Article();
		article.setTitle(title);
		article.setSummary(summary);
		article.setContent(content);
		article.setCreateUser(GBookConstants.ANONYMOUS_USER_ID);
		article.setCreateTime(DateTool.getCurDate());
		//保存Article
		articleService.save(article);
		return "r:list/1";
	}
	
	/**
	 * 文章预览
	 * @param inv
	 * @param article
	 * @return
	 * @throws Exception
	 */
	@Post("preview")
	public String detail(Invocation inv, Article article) throws Exception
	{
		inv.addModel("article", article);
		return "/article/detail";
	}
	
	/**
	 * 文章阅读
	 */
	@Get("view")
	public String detail() throws Exception
	{
		return "/article/view/1";
	}
	
	/**
	 * 文章阅读
	 * @param model
	 * @param artId
	 * @return
	 * @throws Exception
	 */
	@Get("view/{artId:[0-9]+}")
	public String detail(Model model, @Param("artId") int artId) throws Exception
	{
		Article article = articleService.queryById(artId);
		model.add("article", article);
		return "article/detail";
	}
	
	@Get("list")
	public String list() throws Exception
	{
		return "/article/list/1";
	}
	
	/**
	 * 文章列表
	 * @param model
	 * @param pageSize
	 * @param pageNo
	 * @return
	 * @throws Exception
	 */
	@Get("list/{pageNo:[0-9]+}")
	@Post("list/{pageNo:[0-9]+}")
	public String list(Model model, @Param("pageSize") int pageSize, @Param("pageNo") int pageNo) throws Exception
	{
		if(pageSize == 0)
		{
			pageSize = GBookConstants.DEFAULT_PAGE_SIZE;
		}
		List<Article> articles = articleService.queryListByPage(pageSize, pageNo);
		int total = articleService.queryTotal();
		PageView pageView = new PageView(pageSize, pageNo*pageSize, total, 5);
		model.add("pageView", pageView);
		model.add("articles", articles);
		return "article/list";
	}
	
	/**
	 * 排行榜
	 * @return
	 * @throws Exception
	 */
	@Get("topn")
	public String topn() throws Exception
	{
		return "article/topn";
	}
}
